import {
  saleRestrictOptions,
  packageLayersOptions,
  skuOriginalOptions,
  logisticsStockOptions,
  SELL_WELL_OPTIONS
} from './skuOptionsList';
import { IS_NATION_STANDARD_OPTIONS } from '@/constants/commodity';
import { generateHighlightNode } from '@/utils/bizUtils';
export const modifySkuInfoColumnsFn = (data) => {
  let {
    isView,
    handleChooseMainUnit,
    handleChooseOriginalPlace
  } = data;
  let columns = [
    {
      title: 'SPU编码',
      key: 'spuNo',
      align: 'center',
    },
    {
      title: 'SKU编码',
      key: 'skuNo',
      align: 'center',
    },
    {
      title: '商品名称',
      key: 'productNameFinal',
      align: 'center',
    },
    {
      title: '规格',
      align: 'center',
      key: 'specDetailStr',
    },
    {
      title: '计量单位',
      key: 'unitName'
    },
    {
      title: '数量小数位',
      children: [
        {
          title: '原值',
          key: 'unitDecimals',
        },
        getPresentValueFn({
          isView,
          oldKey: 'unitDecimals',
          key: 'unitDecimalsUpdate',
          attr: {
            type: 'inputNumChange',
            width: 160,
            controls: false,
            max: 3,
            precision: 0
          }
        })
      ]
    },
    {
      title: '是否国标',
      required: true,
      options: IS_NATION_STANDARD_OPTIONS,
      children: [
        {
          title: '原值',
          render: (h, { row }) => <div>{getSelectTextFn({ val: row.nationalStandard, options: IS_NATION_STANDARD_OPTIONS })}</div>
        },
        getPresentValueFn({
          isView,
          oldKey: 'nationalStandard',
          key: 'nationalStandardUpdate',
          options: IS_NATION_STANDARD_OPTIONS,
          attr: {
            type: 'select',
            width: 160
          }
        })
      ]
    },
    {
      title: '是否畅销',
      options: SELL_WELL_OPTIONS,
      children: [
        {
          title: '原值',
          render: (h, { row }) => <div>{getSelectTextFn({ val: row.sellWellFlag, options: SELL_WELL_OPTIONS })}</div>
        },
        getPresentValueFn({
          isView,
          oldKey: 'sellWellFlag',
          key: 'sellWellFlagUpdate',
          options: SELL_WELL_OPTIONS,
          attr: {
            type: 'select',
            width: 160
          }
        })
      ]
    },
    {
      title: '安全率上限',
      required: true,
      children: [
        {
          title: '原值',
          key: 'safeRateUpperLimit',
        },
        {
          title: '现值',
          key: 'safeRateUpperLimitUpdate'
        }
      ]
    },
    {
      title: '安全率下限',
      required: true,
      children: [
        {
          title: '原值',
          key: 'safeRateLowerLimit',
        },
        {
          title: '现值',
          key: 'safeRateLowerLimitUpdate'
        }
      ]
    },
    {
      title: '商品条形码',
      align: 'center',
      children: [
        {
          title: '原值',
          key: 'barcode',
          align: 'center'
        },
        getPresentValueFn({
          isView,
          oldKey: 'barcode',
          key: 'barcodeUpdate',
          attr: {
            type: 'input',
            width: 160
          }
        })
      ]
    },
    {
      title: '最小起售量',
      required: true,
      align: 'center',
      children: [
        {
          title: '原值',
          key: 'saleMin',
          align: 'center'
        },
        getPresentValueFn({
          isView,
          oldKey: 'saleMin',
          key: 'saleMinUpdate',
          attr: {
            type: 'inputSpecialNum',
            width: 160
          }
        })
      ]
    },
    {
      title: '销售限制类型',
      required: true,
      align: 'center',
      options: saleRestrictOptions,
      children: [
        {
          title: '原值',
          align: 'center',
          width: 160,
          render: (h, { row }) => {
            return (
              <div>
                { getSelectTextFn({ val: row.saleRestrict, options: saleRestrictOptions }) }
              </div>
            )
          }
        },
        getPresentValueFn({
          isView,
          options: saleRestrictOptions,
          oldKey: 'saleRestrict',
          key: 'saleRestrictUpdate',
          attr: {
            type: 'select',
            width: 160
          }
        })
      ]
    },
    {
      title: '存量属性',
      options: logisticsStockOptions,
      children: [
        {
          title: '原值',
          key: 'logisticsStock',
          render: (h, { row }) => {
            return (
              <div>
                { getSelectTextFn({ val: row.logisticsStock, options: logisticsStockOptions }) }
              </div>
            )
          }
        },
        getPresentValueFn({
          isView,
          options: logisticsStockOptions,
          oldKey: 'logisticsStock',
          key: 'logisticsStockUpdate',
          attr: {
            type: 'select',
            width: 160,
            clearable: true
          }
        })
      ]
    },
    {
      title: '商品信息备注',
      children: [
        {
          title: '原值',
          key: 'skuRemark',
        },
        getPresentValueFn({
          isView,
          oldKey: 'skuRemark',
          key: 'skuRemarkUpdate',
          attr: {
            type: 'inputHasMaxLength',
            width: 160,
            maxlength: 500
          }
        })
      ]
    },
    {
      title: '包装清单',
      required: true,
      children: [
        {
          title: '原值',
          key: 'packageContents',
        },
        getPresentValueFn({
          isView,
          oldKey: 'packageContents',
          key: 'packageContentsUpdate',
          attr: {
            type: 'inputHasMaxLength',
            width: 160,
            maxlength: 200
          }
        })
      ]
    },
    {
      title: '产地',
      required: true,
      children: [
        {
          title: '原值',
          align: 'center',
          key: 'producingAreaName'
        },
        getPresentValueFn({
          isView,
          oldKey: 'producingAreaName',
          key: 'producingAreaNameUpdate',
          width: 160,
          renderComp: {
            render: (h, { row, index }) => {
              return (
                <el-input
                  prefix-icon="el-icon-search"
                  readonly
                  value={ row.producingAreaNameUpdate }
                  onFocus={ () => { handleChooseOriginalPlace({ index }) } }
                />
              )
            }
          }
        })
      ]
    },
    {
      title: 'Sku来源',
      required: true,
      options: skuOriginalOptions,
      children: [
        {
          title: '原值',
          key: 'skuSource',
          render: (h, { row }) => {
            return (
              <div>
                { getSelectTextFn({ val: row.skuSource, options: skuOriginalOptions }) }
              </div>
            )
          }
        },
        getPresentValueFn({
          isView,
          options: skuOriginalOptions,
          oldKey: 'skuSource',
          key: 'skuSourceUpdate',
          attr: {
            type: 'select',
            width: 160
          }
        })
      ]
    },
    {
      title: '来源详情',
      children: [
        {
          title: '原值',
          key: 'skuSourceDesc',
        },
        getPresentValueFn({
          isView,
          oldKey: 'skuSourceDesc',
          key: 'skuSourceDescUpdate',
          attr: {
            type: 'input',
            width: 160,
          }
        })
      ]
    },
    {
      title: '件装数量',
      required: true,
      align: 'center',
      children: [
        {
          title: '原值',
          key: 'pieceInfo',
          align: 'center'
        },
        getPresentValueFn({
          isView,
          oldKey: 'pieceInfo',
          key: 'pieceInfoUpdate',
          attr: {
            type: 'input',
            width: 160
          }
        })
      ]
    },
    {
      title: '层数',
      align: 'center',
      options: packageLayersOptions,
      children: [
        {
          title: '原值',
          align: 'center',
          width: 160,
          render: (h, { row }) => {
            return (
              <div>
                { getSelectTextFn({ val: row.packageLayers, options: packageLayersOptions }) }
              </div>
            )
          }
        },
        getPresentValueFn({
          isView,
          options: packageLayersOptions,
          oldKey: 'packageLayers',
          key: 'packageLayersUpdate',
          attr: {
            type: 'select',
            width: 160
          }
        })
      ]
    },
    {
      title: '单品包装',
      children: [
        {
          title: '包装单位',
          align: 'center',
          key: 'singlePackageUnit'
        },
        {
          title: '包装数量',
          children: [
            {
              title: '原值',
              align: 'center',
              key: 'singlePackageCount'
            },
            getPresentValueFn({
              isView,
              oldKey: 'singlePackageCount',
              key: 'singlePackageCountUpdate',
              attr: {
                type: 'inputSpecialNum',
                width: 160
              }
            })
          ]
        },
        {
          title: '最长边(cm)',
          children: [
            {
              title: '原值',
              align: 'center',
              key: 'singleEdge1'
            },
            getPresentValueFn({
              isView,
              oldKey: 'singleEdge1',
              key: 'singleEdge1Update',
              attr: {
                type: 'inputNumChange',
                width: 160
              }
            })
          ]
        },
        {
          title: '次长边(cm)',
          children: [
            {
              title: '原值',
              align: 'center',
              key: 'singleEdge2'
            },
            getPresentValueFn({
              isView,
              oldKey: 'singleEdge2',
              key: 'singleEdge2Update',
              attr: {
                type: 'inputNumChange',
                width: 160
              }
            })
          ]
        },
        {
          title: '最短边(cm)',
          children: [
            {
              title: '原值',
              align: 'center',
              key: 'singleEdge3'
            },
            getPresentValueFn({
              isView,
              oldKey: 'singleEdge3',
              key: 'singleEdge3Update',
              attr: {
                type: 'inputNumChange',
                width: 160
              }
            })
          ]
        },
        {
          title: '体积(m³)',
          children: [
            {
              title: '原值',
              align: 'center',
              key: 'singleVolume'
            },
            {
              title: '现值',
              align: 'center',
              key: 'singleVolumeUpdate',
              render: (h, { row }) => {
                return generateHighlightNode(h, row, 'singleVolume')
              }
            }
          ]
        },
        {
          title: '重量(kg)',
          children: [
            {
              title: '原值',
              align: 'center',
              key: 'singleWeight'
            },
            getPresentValueFn({
              isView,
              oldKey: 'singleWeight',
              key: 'singleWeightUpdate',
              attr: {
                type: 'inputSpecialNum',
                width: 160
              }
            })
          ]
        },
        {
          title: '是否物流已测量',
          key: 'logisticsMeasured'
        }
      ]
    },
    {
      title: '一层包装',
      children: [
        {
          title: '包装单位',
          children: [
            {
              title: '原值',
              align: 'center',
              key: 'onePackageUnit'
            },
            getPresentValueFn({
              isView,
              oldKey: 'onePackageUnit',
              key: 'onePackageUnitUpdate',
              renderComp: {
                render: (h, { row, index }) => {
                  return (
                    <el-input
                      prefix-icon="el-icon-search"
                      readonly
                      value={ row.onePackageUnitUpdate }
                      onFocus={ () => { handleChooseMainUnit({ type: 'onePackageUnitUpdate', index }) } }
                    />
                  )
                }
              }
            })
          ]
        },
        {
          title: '包装数量',
          children: [
            {
              title: '原值',
              align: 'center',
              key: 'onePackageCount'
            },
            getPresentValueFn({
              isView,
              oldKey: 'onePackageCount',
              key: 'onePackageCountUpdate',
              attr: {
                type: 'inputSpecialNum',
                width: 160
              }
            })
          ]
        },
        {
          title: '最长边(cm)',
          children: [
            {
              title: '原值',
              align: 'center',
              key: 'oneEdge1'
            },
            getPresentValueFn({
              isView,
              oldKey: 'oneEdge1',
              key: 'oneEdge1Update',
              attr: {
                type: 'inputNumChange',
                width: 160
              }
            })
          ]
        },
        {
          title: '次长边(cm)',
          children: [
            {
              title: '原值',
              align: 'center',
              key: 'oneEdge2'
            },
            getPresentValueFn({
              isView,
              oldKey: 'oneEdge2',
              key: 'oneEdge2Update',
              attr: {
                type: 'inputNumChange',
                width: 160
              }
            })
          ]
        },
        {
          title: '最短边(cm)',
          children: [
            {
              title: '原值',
              align: 'center',
              key: 'oneEdge3'
            },
            getPresentValueFn({
              isView,
              oldKey: 'oneEdge3',
              key: 'oneEdge3Update',
              attr: {
                type: 'inputNumChange',
                width: 160
              }
            })
          ]
        },
        {
          title: '体积(m³)',
          children: [
            {
              title: '原值',
              align: 'center',
              key: 'oneVolume'
            },
            {
              title: '现值',
              align: 'center',
              key: 'oneVolumeUpdate',
              render: (h, { row }) => {
                return generateHighlightNode(h, row, 'oneVolume')
              }
            }
          ]
        },
        {
          title: '重量(kg)',
          children: [
            {
              title: '原值',
              align: 'center',
              key: 'oneWeight'
            },
            getPresentValueFn({
              isView,
              oldKey: 'oneWeight',
              key: 'oneWeightUpdate',
              attr: {
                type: 'inputSpecialNum',
                width: 160
              }
            })
          ]
        },
      ]
    },
    {
      title: '二层包装',
      children: [
        {
          title: '包装单位',
          children: [
            {
              title: '原值',
              align: 'center',
              key: 'twoPackageUnit'
            },
            getPresentValueFn({
              isView,
              oldKey: 'twoPackageUnit',
              key: 'twoPackageUnitUpdate',
              renderComp: {
                render: (h, { row, index }) => {
                  return (
                    <el-input
                      prefix-icon="el-icon-search"
                      readonly
                      value={ row.twoPackageUnitUpdate }
                      onFocus={ () => { handleChooseMainUnit({ type: 'twoPackageUnitUpdate', index }) } }
                    />
                  )
                }
              }
            })
          ]
        },
        {
          title: '包装数量',
          children: [
            {
              title: '原值',
              align: 'center',
              key: 'twoPackageCount'
            },
            getPresentValueFn({
              isView,
              oldKey: 'twoPackageCount',
              key: 'twoPackageCountUpdate',
              attr: {
                type: 'inputSpecialNum',
                width: 160
              }
            })
          ]
        },
        {
          title: '最长边(cm)',
          children: [
            {
              title: '原值',
              align: 'center',
              key: 'twoEdge1'
            },
            getPresentValueFn({
              isView,
              oldKey: 'twoEdge1',
              key: 'twoEdge1Update',
              attr: {
                type: 'inputNumChange',
                width: 160
              }
            })
          ]
        },
        {
          title: '次长边(cm)',
          children: [
            {
              title: '原值',
              align: 'center',
              key: 'twoEdge2'
            },
            getPresentValueFn({
              isView,
              oldKey: 'twoEdge2',
              key: 'twoEdge2Update',
              attr: {
                type: 'inputNumChange',
                width: 160
              }
            })
          ]
        },
        {
          title: '最短边(cm)',
          children: [
            {
              title: '原值',
              align: 'center',
              key: 'twoEdge3'
            },
            getPresentValueFn({
              isView,
              oldKey: 'twoEdge3',
              key: 'twoEdge3Update',
              attr: {
                type: 'inputNumChange',
                width: 160
              }
            })
          ]
        },
        {
          title: '体积(m³)',
          children: [
            {
              title: '原值',
              align: 'center',
              key: 'twoVolume'
            },
            {
              title: '现值',
              align: 'center',
              key: 'twoVolumeUpdate',
              render: (h, { row }) => {
                return generateHighlightNode(h, row, 'twoVolume')
              }
            }
          ]
        },
        {
          title: '重量(kg)',
          children: [
            {
              title: '原值',
              align: 'center',
              key: 'twoWeight'
            },
            getPresentValueFn({
              isView,
              oldKey: 'twoWeight',
              key: 'twoWeightUpdate',
              attr: {
                type: 'inputSpecialNum',
                width: 160
              }
            })
          ]
        },
      ]
    },
    {
      title: '三层包装',
      children: [
        {
          title: '包装单位',
          children: [
            {
              title: '原值',
              align: 'center',
              key: 'threePackageUnit'
            },
            getPresentValueFn({
              isView,
              oldKey: 'threePackageUnit',
              key: 'threePackageUnitUpdate',
              renderComp: {
                render: (h, { row, index }) => {
                  return (
                    <el-input
                      prefix-icon="el-icon-search"
                      readonly
                      value={ row.threePackageUnitUpdate }
                      onFocus={ () => { handleChooseMainUnit({ type: 'threePackageUnitUpdate', index }) } }
                    />
                  )
                }
              }
            })
          ]
        },
        {
          title: '包装数量',
          children: [
            {
              title: '原值',
              align: 'center',
              key: 'threePackageCount'
            },
            getPresentValueFn({
              isView,
              oldKey: 'threePackageCount',
              key: 'threePackageCountUpdate',
              attr: {
                type: 'inputSpecialNum',
                width: 160
              }
            })
          ]
        },
        {
          title: '最长边(cm)',
          children: [
            {
              title: '原值',
              align: 'center',
              key: 'threeEdge1'
            },
            getPresentValueFn({
              isView,
              oldKey: 'threeEdge1',
              key: 'threeEdge1Update',
              attr: {
                type: 'inputNumChange',
                width: 160
              }
            })
          ]
        },
        {
          title: '次长边(cm)',
          children: [
            {
              title: '原值',
              align: 'center',
              key: 'threeEdge2'
            },
            getPresentValueFn({
              isView,
              oldKey: 'threeEdge2',
              key: 'threeEdge2Update',
              attr: {
                type: 'inputNumChange',
                width: 160
              }
            })
          ]
        },
        {
          title: '最短边(cm)',
          children: [
            {
              title: '原值',
              align: 'center',
              key: 'threeEdge3'
            },
            getPresentValueFn({
              isView,
              oldKey: 'threeEdge3',
              key: 'threeEdge3Update',
              attr: {
                type: 'inputNumChange',
                width: 160
              }
            })
          ]
        },
        {
          title: '体积(m³)',
          children: [
            {
              title: '原值',
              align: 'center',
              key: 'threeVolume'
            },
            {
              title: '现值',
              align: 'center',
              key: 'threeVolumeUpdate',
              render: (h, { row }) => {
                return generateHighlightNode(h, row, 'threeVolume')
              }
            }
          ]
        },
        {
          title: '重量(kg)',
          children: [
            {
              title: '原值',
              align: 'center',
              key: 'threeWeight'
            },
            getPresentValueFn({
              isView,
              oldKey: 'threeWeight',
              key: 'threeWeightUpdate',
              attr: {
                type: 'inputSpecialNum',
                width: 160
              }
            })
          ]
        },
      ]
    }
  ]
  if (!isView) {
    columns.push({
      title: '操作',
      key: 'action',
      align: 'center',
      fixed: 'right'
    })
  }
  return columns
}

/* 获取现值 */
export const getPresentValueFn = (data) => {
  let { isView, optionsKey, options, renderComp, oldKey, key, attr } = data;
  /**
   * @viewCol 产看状态的现值
   * @editCol 可编辑状态的现值
  */
  let viewCol = {
    title: '现值',
    align: 'center',
    key,
    render: (h, { row }) => {
      return generateHighlightNode(h, row, oldKey)
    }
  }
  /* 如果是下拉框，需要进行回显操作特殊处理 */
  if (optionsKey) { // 接口返回的options处理逻辑
    viewCol = {
      title: '现值',
      align: 'center',
      render: (h, { row }) => {
        return generateHighlightNode(h, row, oldKey, getSelectTextFn({ val: row[key], options: row[optionsKey] }))
      }
    }
  }
  if (options) { // 写死的options处理逻辑
    viewCol = {
      title: '现值',
      align: 'center',
      render: (h, { row }) => {
        return generateHighlightNode(h, row, oldKey, getSelectTextFn({ val: row[key], options }))
      }
    }
  }
  let editCol = {
    title: '现值',
    align: 'center',
    key,
    ...attr
  }
  if (renderComp) { // 如果有eventMethods方法，就特殊渲染render
    editCol = {
      title: '现值',
      align: 'center',
      key,
      ...renderComp
    }
  }
  let result = isView ? viewCol : editCol;
  return result;
}

/* select下拉框value对应label的回显方法 */
const getSelectTextFn = (data) => {
  let { val, options } = data;
  let result = val;
  let target = _.find(options, (item) => {
    return item.optionValue === val || item.value === val;
  });
  if (target) {
    result = target.optionHtml || target.label;
  }
  return result;
}
